package com.keon.jianbao.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.enu.MyInfoEdit;
import com.keon.jianbao.util.StringUtils;

import butterknife.Bind;

/**
 * Created by keon on 15/9/6.
 */
public class MyInfoEditActivity extends BaseActivity {

    @Bind(R.id.my_info_edit)
    EditText etText;
    @Bind(R.id.my_info_edit_save)
    Button bSave;

    int type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_info_edit;
    }

    @Override
    protected int getToolBarTitle() {
        return MyInfoEdit.getTitle(type);
    }

    @Override
    public void initView() {
        bSave.setOnClickListener(this);

        Bundle bundle = getIntent().getExtras();
        type = bundle.getInt("type");

        if (type == MyInfoEdit.ADDRESS.getId()) {
            etText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
            etText.setSingleLine(false);
            etText.setMinLines(3);
        } else {
            etText.setSingleLine();
        }
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        String text = bundle.getString("text");
        if (!StringUtils.isEmpty(text)) {
            etText.setText(text);
            etText.setSelection(text.length());
        }

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.my_info_edit_save:
                if (type == MyInfoEdit.MOBILE.getId()) {
                    if (!StringUtils.isMobile(etText.getText().toString())) {
                        showToast("请输入正确的电话号码");
                        break;
                    }
                }
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("text", etText.getText().toString());
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                onBackPressed();
                break;
        }
    }
}
